package com.fjh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjh.domain.Prod;
import com.fjh.domain.ProdProp;
import com.fjh.domain.Sku;
import com.fjh.model.StockChange;
import com.fjh.service.ProdService;
import com.fjh.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("prod/prod")
@Api(tags = "商品管理")
public class ProdController {
    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;

    @GetMapping("page")
    @ApiOperation("分页查询商品")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public ResponseEntity<Page<Prod>> loadProdPage(Page<Prod> page,Prod prod){
        Page<Prod> prodPage = prodService.loadProdPage(page,prod);
        return ResponseEntity.ok(prodPage);
    }

    @PostMapping
    @ApiOperation("新增商品")
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public ResponseEntity<Void> save(@RequestBody Prod prod){
        prodService.save(prod);
        return ResponseEntity.ok().build();
    }

    //前台商城的接口
    @GetMapping("prod/prodInfo")
    @ApiOperation("根据商品的id查询前台商品信息")
    public ResponseEntity<Prod> loadProdPage(Long prodId){
        Prod prod = prodService.findFrontProdSku(prodId);
        return ResponseEntity.ok(prod);
    }

    /**
     * 远程调用根据商品id查询商品信息
     * @param prodId
     * @return
     */
    @GetMapping("findProdById")
    @ApiOperation("根据商品id查询商品信息")
    Prod findProdById(@RequestParam Long prodId){
        return prodService.getById(prodId);
    }

    /**
     * 远程调用  根据skuIds查询sku集合
     * @param skuIds
     * @return
     */
    @GetMapping("getSkuListByIds")
    @ApiOperation("根据skuIds查询sku集合")
    List<Sku> getSkuListByIds(@RequestBody List<Long> skuIds){
        return skuService.listByIds(skuIds);
    }

    /**
     * 发起远程调用   修改mysql的库存
     * @param stockChange
     */
    @PostMapping("changeStock")
    @ApiOperation("修改prod和sku的库存")
    void changeStock(@RequestBody StockChange stockChange){
        prodService.changeStock(stockChange);
    }

}
